package io.renren.modules.app.controller;

import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.form.UserOrderForm;
import io.renren.modules.app.service.OrderService;
import io.renren.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/app/order")
@Api("订单业务")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private JwtUtils jwtUtils;
    @Login
    @RequestMapping("/searchUserOrderList")
    @ApiOperation(value = "用户查询订单")
    public R searchUserOrderList(@RequestBody UserOrderForm userOrderForm, @RequestHeader HashMap header){
        //检验参数
        //ValidatorUtils.validateEntity(userOrderForm);
        //获取token
        String token=header.get("token").toString();
        //从token中取出userid
        int userId = Integer.parseInt(jwtUtils.getClaimByToken(token).getSubject());
        Integer page = userOrderForm.getPage();
        Integer length = userOrderForm.getLength();
        int start = (page - 1) * length;
        HashMap map=new HashMap();
        map.put("userId", userId);
        map.put("start", start);
        map.put("length", length);
        //调用订单查询
        ArrayList<HashMap> list = orderService.searchUserOrderList(map);
        return R.ok().put("list", list);
    }

}
